/**
 * * 题目名称：化栈为队
 * * 题目地址：https://leetcode-cn.com/problems/implement-queue-using-stacks-lcci
 */

// * 思路：使用两个栈，一个栈负责入栈，一个栈负责出栈。

/*
题目：
实现一个MyQueue类，该类用两个栈来实现一个队列。

示例：
MyQueue queue = new MyQueue();
queue.push(1);
queue.push(2);
queue.peek();  // 返回 1
queue.pop();   // 返回 1
queue.empty(); // 返回 false
*/

/**
 * Initialize your data structure here.
 */
 var MyQueue = function() {
    this.stackA = [];
    this.stackB = [];
};

/**
 * Push element x to the back of queue. 
 * @param {number} x
 * @return {void}
 */
MyQueue.prototype.push = function(x) {
    this.stackA.push(x);
};

/**
 * Removes the element from in front of queue and returns that element.
 * @return {number}
 */
MyQueue.prototype.pop = function() {
    if(this.stackB.length){
        return this.stackB.pop();
    }else{
        while(this.stackA.length){
            this.stackB.push(this.stackA.pop());
        }
        if(this.stackB.length){
            return this.stackB.pop();
        }else
        return -1;
    }
};

/**
 * Get the front element.
 * @return {number}
 */
MyQueue.prototype.peek = function() {
    if(this.stackB.length){
        return this.stackB[this.stackB.length-1];
    } else {
        while(this.stackA.length){
            this.stackB.push(this.stackA.pop());
        }
        if(this.stackB.length){
            return this.stackB[this.stackB.length-1]
        }else
        return -1;
    }
};

/**
 * Returns whether the queue is empty.
 * @return {boolean}
 */
MyQueue.prototype.empty = function() {
    return !this.stackA.length && !this.stackB.length;
};

/**
 * Your MyQueue object will be instantiated and called as such:
 * var obj = new MyQueue()
 * obj.push(x)
 * var param_2 = obj.pop()
 * var param_3 = obj.peek()
 * var param_4 = obj.empty()
 */

// 测试用例

console.time('执行用时');
var obj = new MyQueue();
obj.push(1);
obj.push(2);
console.log(obj.peek());
console.log(obj.pop());
console.log(obj.empty());
console.timeEnd('执行用时');